package com.example.license.controller;

import com.example.license.LicenseUtils.UploadUtils;
import com.example.license.entity.LicenseCheckModel;
import com.example.license.service.IAbstractServerInfos;
import com.example.license.service.ILicenseService;
import com.example.license.service.impl.LinuxServiceImpl;
import com.example.license.service.impl.WindowsServerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 */
@Slf4j
@RestController
@RequestMapping("/api/license")
public class LoginController {
    @Autowired
    private ILicenseService service;

    /**
     * 模拟登录验证
     */
    @GetMapping(value = "/login")
    public Map<String, Object> login() {
        Map<String, Object> result = new HashMap<>(2);
        //模拟登录
        result.put("code", 200);
        result.put("message", "成功");
        return result;
    }

    /**
     * @return com.example.license.entity.LicenseCheckModel
     * @Description 获取当前系统的所有Mac, IP地址信息
     * @Author YJ
     * @Date 2022/3/26 21:47
     * @Param [osName]
     **/
    @GetMapping(value = "/getServerInfos")
    public LicenseCheckModel getServerInfos(@RequestParam(value = "osName", required = false) String osName) {
        //操作系统类型
        if (StringUtils.isEmpty(osName)) {
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();
        IAbstractServerInfos abstractServerInfos = null;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerImpl();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServiceImpl();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServiceImpl();
        }
        return abstractServerInfos.getServerInfos();
    }

    /**
     * @return java.util.Map<java.lang.Object, java.lang.Object>
     * @Description 证书上传
     * @Author YJ
     * @Date 2022/3/24 17:50
     * @Param [multiReq]
     **/
    @PostMapping(value = "/uploadFiles")
    public Map<String, Object> uploadFiles(MultipartFile file) {
        Map<String, Object> map = new HashMap<String, Object>(2);
        if (file.isEmpty()) {
            map.put("code", "400");
            map.put("message", "文件未选择，请选择文件");
            return map;
        }
        // 拿到文件名
        String filename = file.getOriginalFilename();
        // 存放上传图片的文件夹
        File fileDir = UploadUtils.getDirFile();
        // 输出文件夹绝对路径 – 这里的绝对路径是相当于当前项目的路径而不是“容器”路径
        try {
            // 构建真实的文件路径
            File newFile = new File(fileDir.getAbsolutePath() + File.separator + filename);
            // 上传图片到 -》 “绝对路径”
            file.transferTo(newFile);
            map.put("code", "200");
            map.put("message", "上传成功!");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("code", "400");
            map.put("message", "失败!");
        }
        return map;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Description 获取当前系统安装的证书信息
     * @Author YJ
     * @Date 2022/3/27 17:31
     * @Param []
     **/
    @GetMapping("/getCertificateInfo")
    public Map<String, Object> getCertificateInfo() {
        return service.getCertificateInfo();
    }

    /**
     * @return R<Object>
     * @description: 安装证书
     * @author YangJian
     * @date 2022/4/18 10:34
     */
    @GetMapping("/installLicense")
    public Map<String, Object> installLicense() {
        Map<String, Object> map = new HashMap<>(2);
        try {
            service.install();
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "400");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * @param mac
     * @return R<Object>
     * @description: Linux系统获取指定网卡Mac地址
     * @author YangJian
     * @date 2022/4/18 10:35
     */
    @GetMapping("/getAssignMacAddress")
    public String getAssignMacAddress(@RequestParam("mac") String mac) {
        String macAddress = null;
        String command = "cat /sys/class/net/" + mac + "/address";
        String str = "";
        try {
            //linux下查询网卡mac地址的命令
            Process pp = Runtime.getRuntime().exec(command);
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macAddress = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return macAddress;
    }


    /**
     * @return R<Object>
     * @description: 卸载证书
     * @author YangJian
     * @date 2022/4/22 11:50
     */
    @GetMapping("/unInstall")
    public boolean unInstall() {
        return service.unInstall();
    }
}
